package jmine.tec.services.api.export;

import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;

import jmine.tec.services.api.ServicesAPIMessages;

/**
 * EntityExporter abstracto, contém implementação default de alguns métodos.<br/>
 * getExportedEntityClass retorna a classe do exporter por generics, extractAssociations retorna lista vazia.
 * 
 * @author lundberg
 * @param <E>
 */
public abstract class AbstractEntityExporter<E> implements EntityExporter<E> {

    private ExportAction action;

    /**
     * Construtor
     * 
     * @param action action
     */
    public AbstractEntityExporter(ExportAction action) {
        if (action == null) {
            throw new NullPointerException("Exporter não deve ter action nula.");
        }
        this.action = action;
    }

    /**
     * {@inheritDoc}
     */
    public List<Object> exportBefore(E entity) {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    public List<Object> exportAfter(E entity) {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    public final ExporterKey getExporterKey() {
        return new ExporterKey(this.action, this.getExportedEntityClass());
    }

    /**
     * @return the action
     */
    protected ExportAction getAction() {
        return this.action;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    protected Class<E> getExportedEntityClass() {
        try {
            return (Class<E>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        } catch (ClassCastException e) {
            throw new ExportException(ServicesAPIMessages.EXPORTER_CODIFICATION_ERROR.create(this.getClass().getName()), e);
        }
    }
}
